//
//  CWView.m
//  轮播试验
//
//  Created by dllo on 16/5/5.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "CWView.h"
#import "ImageViewCell.h"
#import "UIImageView+WebCache.h"

@interface CWView ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView *collectionV;
@property (nonatomic, strong) NSArray *arr;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) UIPageControl *pageC;
@end
@implementation CWView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
        flow.minimumLineSpacing = 0;
        flow.itemSize = CGSizeMake(self.frame.size.width, self.frame.size.height);
        flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        self.collectionV = [[UICollectionView alloc] initWithFrame:self.frame collectionViewLayout:flow];
        self.collectionV.showsHorizontalScrollIndicator = NO;
        self.collectionV.bounces = NO;
        self.collectionV.pagingEnabled = YES;
        _collectionV.delegate = self;
        _collectionV.dataSource = self;
        [self addSubview:_collectionV];
        [_collectionV registerClass:[ImageViewCell class] forCellWithReuseIdentifier:@"cell"];
        
        self.pageC = [[UIPageControl alloc] initWithFrame:CGRectMake((self.frame.size.width - self.frame.size.width / 2) / 2, self.frame.size.height - self.frame.size.height / 10 * 2, self.frame.size.width / 2, 30)];
        self.pageC.backgroundColor = [UIColor clearColor];
        [self.pageC addTarget:self action:@selector(pageAction) forControlEvents:UIControlEventValueChanged];
        [self addSubview:self.pageC];
    }
    return self;
}
- (void)getImageArr:(NSMutableArray *)array
{
    self.arr = array;
    self.pageC.numberOfPages = self.arr.count;
    self.pageC.currentPage = 0;
    [self setupTimer];
    [_collectionV reloadData];
    _collectionV.contentOffset = CGPointMake(self.frame.size.width, 0);
}
- (void)timerAction
{
    
    [self.collectionV setContentOffset:CGPointMake(self.frame.size.width + self.collectionV.contentOffset.x, 0) animated:YES];
    
    
}
- (void)setupTimer
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}
- (void)pageAction
{
    // 这里是page的点击方法, 不过一般轮播图不用使用 如果需要可以打开
//    [self.collectionV setContentOffset:CGPointMake(self.frame.size.width * (self.pageC.currentPage), 0) animated:YES];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    //实现覆盖效果
    CGFloat content = scrollView.contentOffset.x;
    if (content >= 0 && content <= scrollView.contentSize.width - self.frame.size.width) {
        
        
        NSIndexPath *currentIndexPath = [NSIndexPath indexPathForItem:scrollView.contentOffset.x / [UIScreen mainScreen].bounds.size.width inSection:0];
        NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:scrollView.contentOffset.x / [UIScreen mainScreen].bounds.size.width + 1 inSection:0];
        
        ImageViewCell *cell = (ImageViewCell *)[self.collectionV cellForItemAtIndexPath:currentIndexPath];
        ImageViewCell *cell2 = (ImageViewCell *)[self.collectionV cellForItemAtIndexPath:nextIndexPath];
        CGRect rect = cell.myImageView.frame;
        CGRect rect1 = cell2.myImageView.frame;
        rect.origin.x = 0 + (content - currentIndexPath.item * [UIScreen mainScreen].bounds.size.width);
        rect1.origin.x = 0 - [UIScreen mainScreen].bounds.size.width / 3 + (content - currentIndexPath.item * [UIScreen mainScreen].bounds.size.width) / 3;
        cell.myImageView.frame = rect;
        cell2.myImageView.frame = rect1;
    }
    
 //实现循环滑动
    NSInteger currentPageIndex = scrollView.contentOffset.x / self.frame.size.width;

   
    if ((NSInteger)scrollView.contentOffset.x % (NSInteger)self.frame.size.width == 0) {
        
        if (0 == currentPageIndex) {
            
            currentPageIndex = self.arr.count;
            _collectionV.contentOffset = CGPointMake(self.frame.size.width * currentPageIndex, 0);
            self.pageC.currentPage = currentPageIndex;
            
        } else if (self.arr.count + 1 ==currentPageIndex) {
            
            currentPageIndex = 1;
            _collectionV.contentOffset = CGPointMake(self.frame.size.width * currentPageIndex, 0);
            self.pageC.currentPage = currentPageIndex - 1;
           
        } else {
           self.pageC.currentPage = currentPageIndex - 1;
            
        }
    
    }

    
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [_timer invalidate];
    _timer = nil;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (_timer == nil) {
        
        [self setupTimer];
    }
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
   
    if (indexPath.row == 0) {
        self.DidSelectBlock(self.arr.count);
    } else if (self.arr.count + 1 == indexPath.row) {
        self.DidSelectBlock(0);
    } else {
        self.DidSelectBlock(indexPath.row - 1);
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arr.count + 2;

}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    ImageViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    if (indexPath.row == 0) {
        //cell.backgroundColor = [UIColor redColor];
        [cell.myImageView sd_setImageWithURL:[self.arr objectAtIndex:self.arr.count - 1]];
    }
   else if (indexPath.row == self.arr.count + 1) {
       
        [cell.myImageView sd_setImageWithURL:[self.arr objectAtIndex:0]];
    }
    else {

        [cell.myImageView sd_setImageWithURL:[self.arr objectAtIndex:indexPath.row - 1]];
    }
    

    return cell;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
